package thread;

import java.security.spec.RSAOtherPrimeInfo;

/**
 * @author: 成金乘
 * @date: 2022/12/11 16:21
 * @description:
 */
//有三个线程，分别只能打印A，B和C
//要求按顺序打印ABC，打印10次
public class ThreadDemo6 {
    private static Object lock1 = new Object();
    private static Object lock2 = new Object();
    private static Object lock3 = new Object();

    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(()->{
            try {
                for (int i = 0;i < 10;i++){
                    synchronized (lock1){
                        lock1.wait();
                    }
                    System.out.print("A");
                    synchronized (lock2){
                        lock2.notify();
                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });
        Thread t2 = new Thread(()->{
            try {
                for (int i = 0;i < 10;i++){
                    synchronized (lock2){
                        lock2.wait();
                    }
                    System.out.print("B");

                    synchronized (lock3){
                        lock3.notify();
                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }
        });
        Thread t3 = new Thread(()->{
            try {
                for(int i = 0;i < 10;i++){
                    synchronized (lock3){
                        lock3.wait();
                    }
                    System.out.print("C");
                    System.out.println();
                    synchronized (lock1){
                        lock1.notify();
                    }
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }

        });
        t1.start();
        t2.start();
        t3.start();

        Thread.sleep(100);
        //先启动t1线程
        synchronized (lock1){
            lock1.notify();
        }
    }
}
